package com.example.freshfoodproject.controller;

import com.example.freshfoodproject.entry.Goods;
import com.example.freshfoodproject.entry.User;
import com.example.freshfoodproject.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    /*
    * 前端传入账号密码，通过login()方法进行账号密码的判断，正确返回true，错误返回false
    * */
    @RequestMapping("/login")
    @ResponseBody
    public boolean login(String username,String password){
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        return userService.login(user);
    }

    /*
    * 将注册的信息添加到用户表中，如果用户名表中已存在会注册失败
    * */
    @RequestMapping("/register")
    @ResponseBody
    public boolean register(String username,String password,String email){
        User user = new User();
        user.setUsername(username);
        if (userService.findUsername(user)){
            return false;
        }
        user.setPassword(password);
        user.setEmail(email);
        return userService.register(user);
    }
    /*
    * 根据用户名，查看该用户的所有信息
    * */
    @RequestMapping("/findByUsername")
    @ResponseBody
    public User findByUsername(String username){
        return userService.findByusername(username);
    }


}
